package com.practicesuite.controllayer;

import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;
import java.util.GregorianCalendar;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.User;

import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Panel for viewing general statistics regarding patient health and billing.
 * 
 * @author James
 *
 */
public class ViewStatisticsPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    
    private GregorianCalendar calendarNow;
    private int yearNow;
    private String[] years;
    
    double numPatients;
    double billingAmount;

    /**
     * Create the panel.
     */
    public ViewStatisticsPanel(MainWindow container, int[] date, final User user) {
        window = container;
        setLayout(new GridLayout(0, 1, 0, 0));
        setPreferredSize(new Dimension(500, 400));

        calendarNow = new GregorianCalendar();
        yearNow = calendarNow.get(GregorianCalendar.YEAR);
        years = new String[] {"Year", Integer.toString(yearNow - 1), Integer.toString(yearNow), 
                Integer.toString(yearNow + 1), Integer.toString(yearNow + 2), Integer.toString(yearNow + 3)};
        
        JPanel panel_8 = new JPanel();
        add(panel_8);
        panel_8.setLayout(new GridLayout(0, 1, 0, 0));
        
        JLabel lblIncomeStatements = new JLabel("Income Statements");
        panel_8.add(lblIncomeStatements);
        lblIncomeStatements.setHorizontalAlignment(SwingConstants.CENTER);
        lblIncomeStatements.setFont(new Font("Tahoma", Font.PLAIN, 24));
        
        JPanel panel_3 = new JPanel();
        panel_8.add(panel_3);
        FlowLayout flowLayout_1 = (FlowLayout) panel_3.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEADING);
        
        final JComboBox comboBoxMonth = new JComboBox();
        comboBoxMonth.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxMonth.setModel(new DefaultComboBoxModel(new String[] {"Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
        panel_3.add(comboBoxMonth);
        
        final JComboBox comboBoxYear = new JComboBox();
        comboBoxYear.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxYear.setModel(new DefaultComboBoxModel(years));
        panel_3.add(comboBoxYear);
        
        JButton btnGoToMonth = new JButton("Go To Month");
        btnGoToMonth.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String month = (String)comboBoxMonth.getSelectedItem();
                String year = (String)comboBoxYear.getSelectedItem();
                
                if (month.equals("Month")) {
                    JOptionPane.showMessageDialog(null, "Please select a month.");
                }
                else if (year.equals("Year")) {
                    JOptionPane.showMessageDialog(null, "Please select a year.");
                }
                else {
                    int[] newDate = new int[2];
                    newDate[1] = Integer.parseInt(year);
                    newDate[0] = comboBoxMonth.getSelectedIndex();
                    window.refresh("viewStatistics", null, newDate, user);
                }
            }
        });
        btnGoToMonth.setHorizontalAlignment(SwingConstants.LEADING);
        btnGoToMonth.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_3.add(btnGoToMonth);
        
        JPanel panel_4 = new JPanel();
        panel_8.add(panel_4);
        panel_4.setLayout(new GridLayout(0, 1, 5, 5));
        
        JPanel panel_6 = new JPanel();
        panel_6.setBackground(Color.WHITE);
        panel_4.add(panel_6);
        panel_6.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel lblNumberOfPatients = new JLabel("Number of Patients Seen: ");
        lblNumberOfPatients.setHorizontalAlignment(SwingConstants.TRAILING);
        lblNumberOfPatients.setFont(new Font("Tahoma", Font.BOLD, 12));
        panel_6.add(lblNumberOfPatients);
        
        JLabel label = new JLabel();
        label.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_6.add(label);
        
        JLabel lblTotalBilledAmount = new JLabel("Total Billed Amount: ");
        lblTotalBilledAmount.setHorizontalAlignment(SwingConstants.TRAILING);
        lblTotalBilledAmount.setFont(new Font("Tahoma", Font.BOLD, 12));
        panel_6.add(lblTotalBilledAmount);
        
        JLabel label_1 = new JLabel();
        label_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_6.add(label_1);
        
        if (date == null) {
            label.setText("[Select a month]");
        }
        else {
            try {
                numPatients = SystemDriver.getTotalPatientsSeen(date[0], date[1]);
                billingAmount = SystemDriver.getTotalBilling(date[0], date[1]);
                System.out.println(date[0] + " " + date[1]);
                label.setText(Double.toString(numPatients));
                label_1.setText(Double.toString(billingAmount));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "System error, contact administrator.");
            }
        }
        
        JPanel panel_5 = new JPanel();
        panel_8.add(panel_5);
        
        JButton btnReturnToHome = new JButton("Return to Home");
        btnReturnToHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int eUID = user.getEUID();
                if (eUID == 0) {
                    window.gotoPanel("adminHome", user);
                }
                else if (eUID == 1) {
                    window.gotoPanel("doctorHome", user);
                }
            }
        });
        btnReturnToHome.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_5.add(btnReturnToHome);
        
        JPanel panel_7 = new JPanel();
        add(panel_7);
        panel_7.setLayout(new GridLayout(0, 1, 0, 0));

    }

}
